﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using SoTaiChinh.Library.Common.SqlServer;
using Entity = SoTaiChinh.Library.Entity;

namespace SoTaiChinh.Library.Data
{
    public class Banner
    {
        public Banner(){}
        Connection db = new Connection();
        
        //Hàm insert giá trị vào bảng Banner
        public bool Insert(Entity.Banner entity)
        {
            bool boolInsert = false;
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_Banner_Insert";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@ViTri", SqlDbType.Int).Value = entity.ViTri;
                SqlCmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 255).Value = entity.Ten;
                SqlCmd.Parameters.Add("@HinhAnh", SqlDbType.NVarChar,255).Value = entity.HinhAnh;
                SqlCmd.Parameters.Add("@LienKet", SqlDbType.NVarChar,255).Value = entity.LienKet;
                SqlCmd.Parameters.Add("@NgayDang", SqlDbType.DateTime).Value = entity.NgayDang;               
                SqlCmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = entity.TrangThai;

                SqlAdp.InsertCommand = SqlCmd;
                SqlAdp.InsertCommand.ExecuteNonQuery();
                SqlCmd.Transaction.Commit();
                boolInsert = true;
                SqlCnn.Close();
            }
            catch
            {
                boolInsert = false;
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return boolInsert;
        }

        //Hàm select tất cả giá trị trong bản banner
        public DataTable Select()
        {
            DataTable dt = new DataTable();
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_Banner_Select";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlAdp.SelectCommand = SqlCmd;
                SqlAdp.SelectCommand.ExecuteNonQuery();
                SqlAdp.Fill(dt);
                SqlCmd.Transaction.Commit();
                SqlCnn.Close();
            }
            catch
            {
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return dt;
        }        

        // Hàm select theo khóa chính của bảng
        public DataTable SelectPrimaryKey(Entity.Banner entity)
        {
            DataTable dt = new DataTable();
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_Banner_SelectPrimaryKey";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;

                SqlAdp.SelectCommand = SqlCmd;
                SqlAdp.SelectCommand.ExecuteNonQuery();
                SqlAdp.Fill(dt);
                SqlCmd.Transaction.Commit();
                SqlCnn.Close();
            }
            catch
            {
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return dt;
        }

        // Hàm select theo vị trí banner của bảng
        public DataTable SelectByViTriBanner(Entity.Banner entity)
        {
            DataTable dt = new DataTable();
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_Banner_SelectByViTriBanner";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@ViTri", SqlDbType.Int).Value = entity.ViTri;

                SqlAdp.SelectCommand = SqlCmd;
                SqlAdp.SelectCommand.ExecuteNonQuery();
                SqlAdp.Fill(dt);
                SqlCmd.Transaction.Commit();
                SqlCnn.Close();
            }
            catch
            {
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return dt;
        }

        // Hàm update dữ liệu cho bảng
        public bool Update(Entity.Banner entity)
        {
            bool boolUpdate = false;
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_Banner_Update";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;
                SqlCmd.Parameters.Add("@ViTri", SqlDbType.Int).Value = entity.ViTri;
                SqlCmd.Parameters.Add("@Ten", SqlDbType.NVarChar, 255).Value = entity.Ten;
                SqlCmd.Parameters.Add("@HinhAnh", SqlDbType.NVarChar, 255).Value = entity.HinhAnh;
                SqlCmd.Parameters.Add("@LienKet", SqlDbType.NVarChar, 255).Value = entity.LienKet;
                SqlCmd.Parameters.Add("@NgayDang", SqlDbType.DateTime).Value = entity.NgayDang;
                SqlCmd.Parameters.Add("@TrangThai", SqlDbType.Bit).Value = entity.TrangThai;

                SqlAdp.UpdateCommand = SqlCmd;
                SqlAdp.UpdateCommand.ExecuteNonQuery();
                SqlCmd.Transaction.Commit();
                boolUpdate = true;
                SqlCnn.Close();
            }
            catch
            {
                boolUpdate = false;
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return boolUpdate;
        }

        // Hàm delete dữ liệu trong bản
        public bool Delete(Entity.Banner entity)
        {
            bool boolDelete = false;
            SqlConnection SqlCnn = db.ReadFromWebConfig();
            SqlDataAdapter SqlAdp = new SqlDataAdapter();
            SqlCommand SqlCmd = new SqlCommand();
            SqlTransaction SqlTrans = SqlCnn.BeginTransaction();
            try
            {
                SqlCmd.Transaction = SqlTrans;
                SqlCmd.Connection = SqlCnn;
                SqlCmd.CommandText = "Proc_Banner_Delete";
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@Id", SqlDbType.Int).Value = entity.Id;

                SqlAdp.DeleteCommand = SqlCmd;
                SqlAdp.DeleteCommand.ExecuteNonQuery();
                SqlCmd.Transaction.Commit();
                boolDelete = true;
                SqlCnn.Close();
            }
            catch
            {
                boolDelete = false;
                SqlCmd.Transaction.Rollback();
            }
            finally
            {
                SqlCmd.Parameters.Clear();
                SqlTrans = null;
            }
            if (SqlCmd != null)
                SqlCmd.Dispose();
            if (SqlAdp != null)
                SqlAdp.Dispose();
            return boolDelete;
        }
    }
}
